package com.yxw.controller;

import com.yxw.pojo.Book;
import com.yxw.service.BookService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;


@Slf4j
@RequestMapping("/books")
@RestController
public class BookController {

    @Autowired
    private BookService bookService;



    //查一本书
    @GetMapping("/{id}")
    public MyResponse getBookById(@PathVariable("id") long id){
        System.out.println(id);

        Book book = bookService.getBookById(id);
//        System.out.println(book.getName());



        return new MyResponse(ResultCode.SUCCESS,book);

    }

    @PostMapping()
    public void getBooksByBook(@RequestBody Book book){


    }

    //查所有书
    @GetMapping()
    public MyResponse getAllBook(@RequestBody Book book){
//        System.out.println(book);
        List<Book> list = new ArrayList<>();
        Book book1 = new Book();
        book1.setId(1);
        book1.setBook_title("数学");
        book1.setBook_summary("国家出版社");
        book1.setBook_author("未知");

        Book book2 = new Book();
        book2.setId(2);
        book2.setBook_title("英语");
        book2.setBook_summary("人民出版社");
        book2.setBook_author("未知");
        list.add(book);
        list.add(book1);
        list.add(book2);
        return new MyResponse(ResultCode.SUCCESS,list);
    }



    @PutMapping()
    public MyResponse updateBook(@RequestBody @Validated Book book){

        System.out.println(book.getBook_title());

        book.setBook_title("java");

        return new MyResponse(ResultCode.SUCCESS,book);
    }


    @DeleteMapping("/{id}")
    public MyResponse deleteBookById(@PathVariable Integer id){
        System.out.println("删除书的id= " + id);

        return new MyResponse(ResultCode.DELETE_WER);

    }


}
